package jplat.tools.string;

import java.util.Enumeration;

import javax.servlet.http.HttpServletRequest;

import jplat.base.constant.KTraceLog;

public class JLogUtils
{
	/**
	 * 显示请求内容
	 * 
	 * @param request
	 */
	public static String getHeadValues( HttpServletRequest request)
	{
		Enumeration e = request.getHeaderNames();

		StringBuffer sbuffer = new StringBuffer();
		sbuffer.append("cli-ip:")
		.append(StringUtil.getIpAddr(request))
		.append("-")
		.append(request.getRemoteHost())
		.append(":").append(request.getRemotePort()).append("\n");
		while (e.hasMoreElements()) {
			String a = (String) e.nextElement();
			sbuffer.append(a).append(":").append(request.getHeader(a)).append("\n");
		}

		return sbuffer.toString();
	}
	
	/**
	 * 日志起始标记.
	 * @author zhangcq
	 * @date Dec 28, 2016
	 * @comment 
	 * @param logAction
	 * @param traceMark
	 * @param userData
	 * @return
	 */
	public static String getStartTraceLog( String logAction, String traceMark, String userData )
	{
		return getTraceLog(logAction,KTraceLog.EVENT_START,traceMark,userData);
	}
	
	/**
	 * 获取标记日志.
	 * @author zhangcq
	 * @date Dec 28, 2016
	 * @comment 
	 * @param logAction
	 * @param traceMark
	 * @param userData
	 * @return
	 */
	public static String getEndTraceLog( String logAction, String traceMark, String userData )
	{
		return getTraceLog(logAction,KTraceLog.EVENT_END,traceMark,userData);
	}

	/**
	 * 日志结束标记.
	 * @author zhangcq
	 * @date Dec 28, 2016
	 * @comment 
	 * @param logAction
	 * @param traceMark
	 * @param userData		用户自定义数据.
	 * @return
	 */
	public static String getTraceLog( String logAction, String event, String traceMark, String userData )
	{
		StringBuffer sbuffer = new StringBuffer();
		
		sbuffer.append(KTraceLog.GRABY_FLAG)
		.append(KTraceLog.ACTION).append(logAction);
		
		if ( StringUtil.isNotEmpty(event) )
		{
			sbuffer.append(KTraceLog.CONNMARK).append(KTraceLog.EVENT).append(event);
		}
		
		sbuffer.append(KTraceLog.CONNMARK).append(KTraceLog.MARK).append(traceMark)
		.append(KTraceLog.CONNMARK).append(KTraceLog.TIME).append(DateUtil.todayStr(DateUtil.FMT_READ_ALL));
		
		if ( StringUtil.isNotEmpty(userData) )
		{
			sbuffer.append(KTraceLog.CONNMARK);
			sbuffer.append(KTraceLog.USER_DATA+userData).append(KTraceLog.USER_DATA_END);
		}
		
		return sbuffer.toString();
	}
	
	/**
	 * zhangcq
	 * 2016-03-15 
	 * @param ex
	 * @return 获取代码级别的错误记录
	 */
	public static String getExceptionLog( Throwable ex, int length, boolean needLine )
	{
		if ( ex == null )
		{
			return "系统异常.";
		}

		StackTraceElement[] stackTraceElements = ex.getStackTrace();
		StringBuffer exLogStr = new StringBuffer();
		if( stackTraceElements != null )
		{
			for(int i =0;i<stackTraceElements.length-length;i++)
			{
				exLogStr.append( stackTraceElements[i].getMethodName()).append(" ");
				exLogStr.append( stackTraceElements[i].getFileName());
				exLogStr.append(":"+stackTraceElements[i].getLineNumber());
				if (needLine)
				{
					exLogStr.append("\n");
				}
				else
				{
					exLogStr.append("|");
				}
			}
		}
		
		return  ex.toString()+ex.getMessage()+" REVOKE_STACK:"+exLogStr.toString();
	}

	public static String buildUserData( String ...args )
	{
		StringBuffer sb = new StringBuffer();
		String connMark = "|";
		for ( int i = 0; i < args.length; ++i )
		{
			
			if ( i == args.length -1 )
			{
				connMark = "";
			}
			
			sb.append(args[i]).append(connMark);
		}
		
		return sb.toString();
	}
	
	public static void main( String args[])
	{
		String mark = JRandomUtil.getRandomSequence(KTraceLog.MARK_LENGTH);
		XLog.log(getTraceLog(KTraceLog.ACTION_CALL_ICOP,KTraceLog.EVENT_SUCCESS,mark,"1234567|1234567890123456|交易成功......"));
		
		XLog.log(buildUserData("A","B","C333"));
		
		try
		{
			new JLogUtils().test();
		}
		catch ( Exception e )
		{
			XLog.log("%s",getExceptionLog(e,0,false));
		}
	}
	
	public  void test()
	{
		double y=1/0;
	}
}
